package com.zhsj.OkeHomeConsumption.ui.activity.entering;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hjq.bar.TitleBar;
import com.orhanobut.hawk.Hawk;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.common.MyActivity;
import com.zhsj.OkeHomeConsumption.empty.FormalUserInfo;
import com.zhsj.OkeHomeConsumption.empty.ReviewBean;
import com.zhsj.OkeHomeConsumption.other.TimeOutHandler;
import com.zhsj.OkeHomeConsumption.ui.activity.MineUserAuthFragment;
import com.zhsj.OkeHomeConsumption.ui.activity.PayAmountActivity;
import com.zhsj.OkeHomeConsumption.ui.activity.entering.mvp.ReviewPersonPresenter;
import com.zhsj.OkeHomeConsumption.ui.activity.entering.mvp.ReviewPersonUIInterFace;
import com.zhsj.OkeHomeConsumption.ui.fragment.minehome.BailActivity;
import com.zhsj.OkeHomeConsumption.utils.JsonUtils;
import com.zhsj.OkeHomeConsumption.utils.LogUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.zhsj.OkeHomeConsumption.common.Constants.SHARED_PERFERENCE_USERINFO;
import static com.zhsj.OkeHomeConsumption.utils.Utils.isFastDoubleClick;

/**
 * author : Xingdx
 * <p>
 * time   : 2018/10/18
 * desc   :合伙人审核
 */
public final class ReviewPartnerActivity extends MyActivity implements ReviewPersonUIInterFace, View.OnClickListener {


    @BindView(R.id.recy_top)
    RecyclerView recyTop;
    private TitleBar tb_about_title;
    private ImageView shenghezhong;

    private ReviewPersonPresenter minePersonPresenter;

    private TextView zhangtai_text;

    private List<ReviewBean> mReviewList = null;


    private ReviewAdapter mReviewadapter = null;

    //交纳保证金
    private RelativeLayout baozhnegjin_rel = null;

    //签署协议
    private LinearLayout xieyi_lin = null;
    private Button yulan_button = null;
    private Button qianshu_button = null;
    private FormalUserInfo userfore;
    private String deposit = "";


    @Override
    protected int getLayoutId() {
        return R.layout.entering_activity_reviewpartner;
    }

    @Override
    protected int getTitleId() {
        return R.id.tb_about_title;
    }

    @Override
    protected void initView() {
        userfore = Hawk.get(Constants.SHARED_PERFERENCE_USERINFO);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        recyTop.setLayoutManager(linearLayoutManager);
        mReviewList = new ArrayList<>();
        zhangtai_text = findViewById(R.id.zhangtai_text);
        minePersonPresenter = new ReviewPersonPresenter(getActivity(), this);
        tb_about_title = (TitleBar) findViewById(R.id.tb_about_title);
        baozhnegjin_rel = findViewById(R.id.baozhnegjin_rel);
        baozhnegjin_rel.setOnClickListener(this);
        shenghezhong = findViewById(R.id.shenghezhong);
        xieyi_lin = findViewById(R.id.xieyi_lin);
        yulan_button = findViewById(R.id.yulan_button);
        yulan_button.setOnClickListener(this);
        qianshu_button = findViewById(R.id.qianshu_button);
        qianshu_button.setOnClickListener(this);
        minePersonPresenter.ReviewPsot("2");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.baozhnegjin_rel:
                //缴纳保证金
                //pay_flage 标识支付类型（0 支付保证金）
                startActivity(new Intent(this,BailActivity.class).putExtra("usertype","2"));

//                startActivity(new Intent(this,PayAmountActivity.class).putExtra("pay_flage",0).putExtra("price",deposit)
//                        .putExtra("usertype","2"));
                break;
            case R.id.yulan_button:
                //预览协议
                minePersonPresenter.ContractPreViewGet("2");
                break;
            case R.id.qianshu_button:
                //签署协议
                if (userfore.getUserAuthentic() != null) {
                    minePersonPresenter.ContractSignGet("2");
                } else {
                    // 实名认证
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("formalUserInfo",userfore);
                    startActivityForResult(new Intent(this, MineUserAuthFragment.class)
                            .putExtras(bundle),1);

                }
                break;
        }
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @Override
    public void ReviewPsot(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONArray jsonarr = jsonOBJ.optJSONArray("data");
                mReviewList = new Gson().fromJson(jsonarr.toString(), new
                        TypeToken<ArrayList<ReviewBean>>() {
                        }.getType());

                for (int i = 0; i < mReviewList.size(); i++) {
                    if (mReviewList.get(i).getShowState() == 0) {
                        mReviewList.remove(i);
                        i--;
                    }
                }
                mReviewadapter = new ReviewAdapter(mReviewList);
                recyTop.setAdapter(mReviewadapter);

            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            Log.e("ReviewPsot", "获取审核状态");
        }
    }

    @Override
    public void BailGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONObject jsonarr = jsonOBJ.optJSONObject("data");
                deposit = jsonarr.optString("depositHigh");
            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            Log.e("BailGet", "获取保证金异常");
        }
    }

    @Override
    public void ContractPreViewGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                String data = jsonOBJ.optString("data");
                startActivityForResult(new Intent(this,ShowSignCompactsFragment.class).putExtra("web_url",data).putExtra("type",2),0);
                if (isFastDoubleClick()) {
                    return;
                }
            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                }
            } else {
                if (!message.equals("null")) {
                }
            }
        } catch (JSONException ex) {
            LogUtils.e("预览电子合同", ex.toString());
        }
    }

    @Override
    public void ContractSignGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                if (isFastDoubleClick()) {
                    return;
                }
                String data = jsonOBJ.optString("data");
                startActivityForResult(new Intent(this,ShowSignCompactsFragment.class).putExtra("web_url",data).putExtra("type",1),0);
            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                }
            } else {
                if (!message.equals("null")) {
                }
            }
        } catch (JSONException ex) {
            LogUtils.e("签署电子合同", ex.toString());
        }
    }

    @Override
    public void DetailGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONObject dataLoginobj = jsonOBJ.optJSONObject("data");
                userfore = JsonUtils.object(dataLoginobj.toString(), FormalUserInfo.class);
                Hawk.put(Constants.SHARED_PERFERENCE_USERINFO, userfore);
            } else {
                if (!message.equals("null")) {
                    toast("message");
                    TimeOutHandler.pDialogUtils.dismiss();
                }
            }
        } catch (JSONException ex) {
            TimeOutHandler.pDialogUtils.dismiss();
        }
    }


    /**
     * 城市合伙人
     * APPLY(1, "申请",1), CHECK(2, "审核",1), MANAGERSIGN(3, "平台签署协议",0),
     * SIGN(4, "用户签署协议",1),DEPOSIT(5, "缴纳保证金",1), OFFLINE(6, "下线",0);
     */
    public class ReviewAdapter extends BaseQuickAdapter<ReviewBean, BaseViewHolder> {

        public ReviewAdapter(@Nullable List<ReviewBean> data) {
            super(R.layout.item_review, data);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder helper, ReviewBean item) {
            switch (item.getFlowType()) {
                case 1:
                    switch (item.getStatus()) {
                        case -1:
                            //失败
                            shenghezhong.setImageResource(R.mipmap.renzhengshibai);//申请提交
                            zhangtai_text.setText("申请失败");
                            break;
                        case 0:
                            //未通过
                            helper.setBackgroundRes(R.id.shenqing_img, R.mipmap.shenqing_hui);
                            shenghezhong.setImageResource(R.mipmap.tijiao);//申请提交
                            zhangtai_text.setText("申请已提交,请耐心等待");
                            break;
                        case 1:
                            //通过
                            helper.setBackgroundRes(R.id.shenqing_img, R.mipmap.shenqing_hei);
                            break;
                    }
                    helper.setText(R.id.tetx, "申请");
                    break;
                case 2:
                    switch (item.getStatus()) {
                        case -1:
                            //失败
                            shenghezhong.setImageResource(R.mipmap.renzhengshibai);
                            zhangtai_text.setText("审核失败");
                            break;
                        case 0:
                            //未通过
                            helper.setBackgroundRes(R.id.shenqing_img, R.mipmap.shenhe_hui);
                            shenghezhong.setImageResource(R.mipmap.tijiao);//申请提交
                            zhangtai_text.setText("正在审核,请耐心等待");
                            break;
                        case 1:
                            //通过
                            helper.setBackgroundRes(R.id.shenqing_img, R.mipmap.shenhe_hei);
                            zhangtai_text.setText("请签署协议");
                            xieyi_lin.setVisibility(View.VISIBLE);
                            break;
                    }
                    helper.setText(R.id.tetx, "审核");
                    break;
                case 4:
                    switch (item.getStatus()) {
                        case -1:
                            //失败
                            shenghezhong.setImageResource(R.mipmap.renzhengshibai);
                            zhangtai_text.setText("审核失败");
                            break;
                        case 0:
                            //未通过
                            helper.setBackgroundRes(R.id.shenqing_img, R.mipmap.qianshuxieyi_hui);
                            break;
                        case 1:
                            //通过
                            helper.setBackgroundRes(R.id.shenqing_img, R.mipmap.qianshuxieyi_hei);
                            baozhnegjin_rel.setVisibility(View.VISIBLE);
                            xieyi_lin.setVisibility(View.GONE);
                            zhangtai_text.setText("请缴纳保证金");
                            minePersonPresenter.BailGet(2);

                            break;
                    }
                    helper.setText(R.id.tetx, "请签署协议");

                    break;
                case 5:
                    switch (item.getStatus()) {
                        case -1:
                            //失败
                            break;
                        case 0:
                            //未通过
                            helper.setBackgroundRes(R.id.shenqing_img, R.mipmap.xinyubaozhengjin_hui);
                            break;
                        case 1:
                            //通过
                            helper.setBackgroundRes(R.id.shenqing_img, R.mipmap.xinyubaozhengjin_hei);
                           finish();
                            break;
                    }
                    helper.setText(R.id.tetx, "请缴纳保证金")
                            .setVisible(R.id.view,false);

                    break;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0){
            minePersonPresenter.ReviewPsot("2");
        }else if(requestCode == 1){
            minePersonPresenter.DetailGet();
        }
    }
}
